package com.wyh.controller;

import com.wyh.common.annotation.Log;
import com.wyh.common.annotation.RateLimiter;
import com.wyh.common.enums.BusinessType;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.ExcelUtils;
import com.wyh.entity.AdminUserLevel;
import com.wyh.request.AdminUserLevelRequest;
import com.wyh.service.IAdminUserLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 用户管理控制层
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "用户管理接口管理",tags = "用户管理接口管理")
@RequestMapping("/admin/adminUserLevel" )
public class AdminUserLevelController {

    private final IAdminUserLevelService iAdminUserLevelService;

    /**
     * 分页查询用户管理列表
     */
    @ApiOperation(value = "分页查询用户管理列表")
    @PostMapping("/pageList")
    public R<PageInfo<AdminUserLevel>> pageList(@RequestBody AdminUserLevelRequest adminUserLevelRequest) {
        return R.success(iAdminUserLevelService.pageList(adminUserLevelRequest));
    }

    /**
     * 获取用户管理详细信息
     */
    @ApiOperation(value = "获取用户管理详细信息")
    @GetMapping(value = "/query/{id}")
    public R<AdminUserLevel> detail(@PathVariable("id") Integer id) {
        return R.success(iAdminUserLevelService.getById(id));
    }

    /**
     * 新增用户管理
     */
    @Log(title = "新增用户管理", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增用户管理")
    @PostMapping("/add")
    public R<Void> add(@RequestBody AdminUserLevelRequest adminUserLevelRequest) {
        iAdminUserLevelService.add(adminUserLevelRequest);
        return R.success();
    }

    /**
     * 修改用户管理
     */
    @Log(title = "修改用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户管理")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody AdminUserLevelRequest adminUserLevelRequest) {
        iAdminUserLevelService.update(adminUserLevelRequest);
        return R.success();
    }

    /**
     * 删除用户管理
     */
    @Log(title = "删除用户管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户管理")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids) {
        iAdminUserLevelService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出用户管理
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody AdminUserLevelRequest adminUserLevelRequest) throws IOException {
        List<AdminUserLevel> list = iAdminUserLevelService.list(adminUserLevelRequest);
        ExcelUtils.exportExcel(list, AdminUserLevel.class, "用户管理信息", response);
    }
}
